package cn.lirenyi.org.controller;

import cn.lirenyi.basic.util.AjaxResult;
import cn.lirenyi.basic.util.PageList;
import cn.lirenyi.org.domain.Systemdictionarydetail;
import cn.lirenyi.org.query.SystemdictionarydetailQuery;
import cn.lirenyi.org.service.ISystemdictionarydetailService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system")
@Api(tags = "产品表",description = "产品表接口详细功能")
public class SystemdictionarydetailController {

    @Autowired
    private ISystemdictionarydetailService service;

    @GetMapping("/{id}")    // 相当于@RequestMapping(value="/id",method = RequestMethod.GET)
    // 根据id查
    public Systemdictionarydetail queryById(@PathVariable("id") Long id){
        return service.queryById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarydetail systemdictionarydetail){
        // 先判断有无id，进而判断是添加还是修改方法
        try {
            // 没有id就是添加
            if (systemdictionarydetail.getId() == null){
                service.add(systemdictionarydetail);
            } else {
                // 有id就是修改
                service.update(systemdictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }

    }

    @DeleteMapping("/{id}")     // 删除
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("艸作失败");
        }
    }

    @PostMapping    // 分页查询
    public PageList<Systemdictionarydetail> queryPage(@RequestBody SystemdictionarydetailQuery systemdictionarydetailQuery){
        PageList<Systemdictionarydetail> page = service.queryPage(systemdictionarydetailQuery);
        System.out.println(page);
        return page;
    }

    @GetMapping    // 查全部
    public List<Systemdictionarydetail> queryAll(){
        return service.queryAll();
    }


    @PatchMapping     // 批量删除
    public AjaxResult patchDel(@RequestBody Long[] id){
        try {
            service.patchDel(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("艸作失败");
        }
    }

}
